package com.lannuokeji.biz;

import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.lannuokeji.base.AController;
import com.lannuokeji.base.exception.RRException;
import com.lannuokeji.utils.FileUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(description="文件上传")
@RestController
@RequestMapping("/file")
public class FileUploadController extends AController {

	@ApiOperation(value = "审核、处理设备申请")
	@PostMapping("/upload")
	public Map<String, Object> a(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
        String fileName = file.getOriginalFilename();
        // 文件后缀
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String saveName = UUID.randomUUID() + suffix;
        // 存储路径
        String fileDir = request.getSession().getServletContext().getRealPath("imgupload/");
        try {
            String filePath = FileUtil.uploadFile(file.getBytes(), fileDir, saveName);
            return setSuccessContent("imgupload/" + saveName);
        } catch (Exception e) {
        	throw new RRException("存储失败");
        }
	}
}
